/**
 *
 */
package com.engine.tools;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;

import com.wfe.db.SessionUtil;
import com.wfe.db.WorkFlowEngineQueryUtil;
import com.wfe.mapping.client.WfPlan;
import com.wfe.mapping.client.WfPlanTask;
import com.wfe.mapping.client.WfProject;
import com.wfe.mapping.client.WfProjectStatus;
import com.wfe.mapping.client.WfProjectTask;

/**
 * @author kareem_yehya
 * @since 19 October 2009 <br>
 *        * @version 1.0.0.1
 * 
 *        This class should be called when add any new task to a Plan which has running projects
 * 
 *        1- Stop the engines 2- update DB with the new task (wf_tasks- Plan Tasks- Routing Table) 3- Use main method and path the Plan task recid as
 *        a parameter. 4- run the engines again when this process finished
 * 
 * */
public class AddPlanTaskToRunningProjects implements Runnable
{

	/**
	 * Member Varialbles
	 * 
	 * */
	static Thread myThread;
	private static long taskNumber = 0;
	private static String planTaskParameter = "";

	int pagingNumber = 1000;
	int firstResult = 0;

	public void run()
	{
		Session session = SessionUtil.getSession();
		WfPlanTask currentPlanTask = WorkFlowEngineQueryUtil.getWfPlanTaskbyID(session, taskNumber);
		WfPlan plan = currentPlanTask.getWfPlan();
		while(true)
		{
			try
			{

				System.out.println("Enter AddPlanTaskToRunningProjects Main Thread");
				session = SessionUtil.getSession();
				List<WfProject> inprocessProjects = getInprocessProjectList(pagingNumber, taskNumber, firstResult, session);
				if(inprocessProjects != null && inprocessProjects.size() > 0)
				{
					for(WfProject project : inprocessProjects)
					{
						System.out.println(" ProjectID= " + project.getRecid());
						Long priority = project.getWfFirePlanAction().getPriority();

						// --------
						createWfProjectTask(currentPlanTask, project, plan, priority, session);

					}// action for end
					firstResult = firstResult + pagingNumber + 1;// get the next
					session.beginTransaction().commit();
					session.close();

				}
				else
				{
					System.out.println(" No Any Inprocess Projects");

					myThread.interrupt();
					return;

				}

				Thread.sleep(500);

			}catch(Exception e)
			{
				e.printStackTrace();
			}
		}

	}

	public List<WfProject> getInprocessProjectList(int maxResults, long taskID, int firstResult, Session session)
	{
		try
		{
			return WorkFlowEngineQueryUtil.getInprocessProjectList(session, maxResults, taskID, firstResult);

		}catch(Exception e)
		{
			e.printStackTrace();
			return null;
		}

	}

	public static void addProjectTask(Session session, WfProjectTask projectTask, WfProject project)
	{
		session.save(projectTask);

		WfProjectStatus wfProjectStatus = (WfProjectStatus) session.get(WfProjectStatus.class, 4L);// under update
		project.setWfProjectStatus(wfProjectStatus);
		session.save(project);

	}

	public WfProjectTask createWfProjectTask(WfPlanTask planTask, WfProject project, WfPlan wfPlan, Long priority, Session session)
	{

		try
		{
			WfProjectTask projectTask = new WfProjectTask();
			projectTask.setRecid(WorkFlowEngineQueryUtil.getRandomID());
			projectTask.setCreationTime(new Date());
			projectTask.setWfPlanTask(planTask);
			projectTask.setPriority(priority);
			projectTask.setWfTask(planTask.getWfTask());
			projectTask.setWfTaskType(planTask.getWfTask().getWfTaskType());

			// -------------------------------------------------------
			projectTask.setWfProject(project);
			projectTask.setIsProcessed(false);
			// ------------------ created status ----------------------------
			projectTask.setWfProjectTaskStatus(WorkFlowEngineQueryUtil.getWfProjectTaskStatusByID(5, session));

			// ------------------ add wf_task-----------------------------

			addProjectTask(session, projectTask, project);
			return null;
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String args[])
	{

		if(args.length > 0)
		{
			planTaskParameter = args[0];

			if(planTaskParameter != null && !planTaskParameter.equals(""))
			{
				taskNumber = Long.parseLong(planTaskParameter);
				System.out.println("Plan task = " + planTaskParameter);
			}

		}
		else
		{
			System.out.println("Please add WF_PLAN_TASK_ID as a parameter" + " to be added to all inprocess projects");
		}
		myThread = new Thread(new AddPlanTaskToRunningProjects());
		myThread.start();

	}

}